package sg.edu.nus.iss.umpire.service.competition;

import javax.persistence.EntityManager;

import sg.edu.nus.iss.umpire.dao.jpa.CompetitionDaoImpl;
import sg.edu.nus.iss.umpire.exception.DAOException;
import sg.edu.nus.iss.umpire.support.HttpMethod;
import sg.edu.nus.iss.umpire.support.Service;
import sg.edu.nus.iss.umpire.support.ServiceConfig;
import sg.edu.nus.iss.umpire.support.ServiceContext;

@ServiceConfig(path = "competitions/list", method = HttpMethod.GET)
public class ListCompetitionService implements Service{

    @Override
    public String serve(ServiceContext ctx) throws Exception {
		ctx.getRequest().setAttribute(JSON_DATA,
				listCompetition(ctx.getEntityManager()));
	return JSON_VIEW;
    }

	private Object listCompetition(EntityManager em) throws DAOException {
		return new CompetitionDaoImpl(em).loadAll();
	}

}
